package cn.dianhun.om.jpa.controller;

import cn.dianhun.om.jpa.model.User;
import cn.dianhun.om.jpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cyt
 * @describe
 * @create 2021-01-06 15:18
 */
@RequestMapping("/api")
@RestController
public class UserController {
    @Autowired
    private UserRepository userRepository;

    /**
     * 保存用户
     *
     * @param user
     * @return
     */
    @PostMapping(path = "user", consumes = {MediaType.APPLICATION_JSON_VALUE})
    public User addNewUser(@RequestBody User user) {
        return userRepository.save(user);
    }

    /**
     * 根据分页信息查询用户
     *
     * @return
     */
    @GetMapping(path = "users")
    @ResponseBody
    public List<User> getAllUsers() {
        List<User> all = userRepository.findAll();
        return all;
    }
}
